;======================================================================
;页面文本更新
Page_Text_Update

    ;标记页面更新中
    JSR Paging_Enable
    
    .IF CURSOR_ELAY_ENABLE
        ;启用光标
        JSR Cursor_Disable
    .ENDIF
    
    LDA #$00
    STA <FC_Page_Item_Index
    STA FC_PPU_Item_Count
.Update_Item
    
    JSR Page_Text_Item_Clear
    JSR Page_Text_Item_Write
    
    ;延迟显示光标处理
    .IF CURSOR_ELAY_ENABLE
        LDA <FC_Item_Index
        SEC
        SBC <FC_Page_Cur_Index
        STA <FC_Data_Temp
        CMP <FC_Page_Item_Index
        BNE .Update_Item_Continue
        ;启用光标
        JSR Cursor_Enable
    .ENDIF

.Update_Item_Continue
    
    INC <FC_Page_Item_Index
    INC FC_PPU_Item_Count
    
    LDA <FC_Page_Item_Index
    CMP #GAME_NAME_PAGE_SIZE
    BCS .Wait_PPU_Task_Finish
    
    LDA FC_PPU_Item_Count
    CMP #GAME_NAME_PPU_BUF_COUNT
    BCC .Update_Item
    
.Wait_PPU_Task_Finish
    LDA FC_PPU_Item_Count
    BNE .Wait_PPU_Task_Finish
    
    LDA <FC_Page_Item_Index
    CMP #GAME_NAME_PAGE_SIZE
    BCC .Update_Item

    ;取消页面更新中标记
    JSR Paging_Disable
    
    ;启用光标
    JSR Cursor_Enable
    
.End
    RTS

;======================================================================
;前一个节目
Game_Select_Pre_Item
    LDA <FC_Item_Index
    BNE .Pre_Item;切换到最大索引
    
    LDA #GAME_COUNT - 1
    STA <FC_Item_Index
    
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        JSR Sound_For_Item_Chage
    .ELSE
        JSR Sound_For_Page_Chage
    .ENDIF
    
    ;记录最后一页
    LDA <FC_Page_Cur_Index
    STA <FC_Page_Last_Index
    
    ;最后页
    LDA #GAME_PAGE_MAX_INDEX
    STA <FC_Page_Cur_Index
    JSR Page_Text_Update_For_Joy
    
    RTS

.Pre_Item
    LDA <FC_Page_Cur_Index
    CMP <FC_Item_Index
    BNE .Pre_Item_Set
    
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        JSR Sound_For_Item_Chage
    .ELSE
        JSR Sound_For_Page_Chage
    .ENDIF
    
    ;记录最后一页
    LDA <FC_Page_Cur_Index
    STA <FC_Page_Last_Index
    
    ;前一页
    LDA <FC_Page_Cur_Index
    SEC
    SBC #GAME_NAME_PAGE_SIZE
    STA <FC_Page_Cur_Index
    DEC <FC_Item_Index
    JSR Page_Text_Update_For_Joy
    
    RTS
    
.Pre_Item_Set
    DEC <FC_Item_Index
    JSR Sound_For_Item_Chage
.End
    RTS

;======================================================================
;下一个节目
Game_Select_Next_Item
    LDA <FC_Item_Index
    CMP #GAME_COUNT - 1
    BCC .Next_Item
    
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        JSR Sound_For_Item_Chage
    .ELSE
        JSR Sound_For_Page_Chage
    .ENDIF
    
    ;记录最后一页
    LDA <FC_Page_Cur_Index
    STA <FC_Page_Last_Index
    
    ;起始页
    LDA #$00
    STA <FC_Page_Cur_Index
    STA <FC_Item_Index
    JSR Page_Text_Update_For_Joy
    RTS
    
.Next_Item
    LDA <FC_Page_Cur_Index
    CLC
    ADC #GAME_NAME_PAGE_SIZE
    SEC
    SBC #$01
    CMP <FC_Item_Index
    BNE .Next_Item_Set
    
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        JSR Sound_For_Item_Chage
    .ELSE
        JSR Sound_For_Page_Chage
    .ENDIF
    
    ;记录最后一页
    LDA <FC_Page_Cur_Index
    STA <FC_Page_Last_Index
    
    ;下一页
    LDA <FC_Page_Cur_Index
    CLC
    ADC #GAME_NAME_PAGE_SIZE
    STA <FC_Page_Cur_Index
    INC <FC_Item_Index
    JSR Page_Text_Update_For_Joy
    
    RTS

.Next_Item_Set
    INC <FC_Item_Index
    JSR Sound_For_Item_Chage
.End
    RTS

;======================================================================
;限制最后一页索引
Game_Select_Limit
    LDA <FC_Page_Cur_Index
    CLC
    ADC <FC_Page_Item_Index
    BCS .Limit_Page_Item_Index  ;索引超过255则限制当前页最大索引
    CMP #GAME_COUNT - 1         ;检查是否大于等于最大节目索引
    BCC .End                    ;小于最大节目索引则不处理
.Limit_Page_Item_Index
    LDA #GAME_COUNT - 1
    SEC
    SBC #GAME_PAGE_MAX_INDEX
    CLC
    ADC <FC_Page_Cur_Index
    STA <FC_Item_Index
.End
    RTS

;======================================================================
;前一页节目
Game_Select_Pre_Page
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        LDA #$00
        STA <FC_Item_Index
        JSR Sound_For_Item_Chage
        RTS
    .ELSE
    
        JSR Sound_For_Page_Chage
        
        LDA <FC_Page_Cur_Index
        BNE .Pre_Page 
    
    ;最后一页
.Last_Page
        LDA <FC_Item_Index
        SEC
        SBC <FC_Page_Cur_Index
        STA <FC_Page_Item_Index

        ;记录最后一页
        LDA <FC_Page_Cur_Index
        STA <FC_Page_Last_Index

        ;最后一页
        LDA #GAME_PAGE_MAX_INDEX
        STA <FC_Page_Cur_Index
        CLC
        ADC <FC_Page_Item_Index
        STA <FC_Item_Index
        
        JSR Game_Select_Limit
        JSR Page_Text_Update_For_Joy
        
        RTS
.Pre_Page
        ;记录最后一页
        LDA <FC_Page_Cur_Index
        STA <FC_Page_Last_Index
        LDA <FC_Page_Cur_Index
        SEC
        SBC #GAME_NAME_PAGE_SIZE
        STA <FC_Page_Cur_Index
        LDA <FC_Item_Index
        SEC
        SBC #GAME_NAME_PAGE_SIZE
        STA <FC_Item_Index
        JSR Page_Text_Update_For_Joy
    .ENDIF
.End
        RTS

;======================================================================
;下一页节目
Game_Select_Next_Page
    .IF GAME_COUNT <= GAME_NAME_PAGE_SIZE
        LDA #GAME_COUNT - 1
        STA <FC_Item_Index
        JSR Sound_For_Item_Chage
        RTS
    .ELSE
    
        JSR Sound_For_Page_Chage
        
        ;检查是否在最后一页
        LDA <FC_Page_Cur_Index
        CMP #GAME_PAGE_MAX_INDEX
        BNE .Next_Page
    
    ;第一页
.First_Page
        ;记录最后一页
        LDA <FC_Page_Cur_Index
        STA <FC_Page_Last_Index
        
        LDA <FC_Item_Index
        SEC
        SBC <FC_Page_Cur_Index
        STA <FC_Item_Index
        LDA #$00
        STA <FC_Page_Cur_Index
        
        .IF GAME_COUNT > GAME_NAME_PAGE_SIZE
            JSR Page_Text_Update
        .ENDIF
    
        RTS

.Next_Page
        ;记录最后一页
        LDA <FC_Page_Cur_Index
        STA <FC_Page_Last_Index
        
        LDA <FC_Item_Index
        SEC
        SBC <FC_Page_Cur_Index
        STA <FC_Page_Item_Index
        
        LDA <FC_Item_Index
        CLC
        ADC #GAME_NAME_PAGE_SIZE
        STA <FC_Item_Index
        
        LDA <FC_Page_Cur_Index
        CLC
        ADC #GAME_NAME_PAGE_SIZE
        STA <FC_Page_Cur_Index
        
        JSR Game_Select_Limit

        .IF GAME_COUNT > GAME_NAME_PAGE_SIZE
            JSR Page_Text_Update
        .ENDIF
    
        RTS
    .ENDIF
    
.End
    RTS

;======================================================================
Page_Text_Update_For_Joy
    .IF GAME_COUNT > GAME_NAME_PAGE_SIZE
        JSR Page_Text_Update
    .ENDIF
    RTS
